﻿package com.tangchao.client.sdh.view
{
	import com.tangchao.client.entities.User;
	import com.tangchao.client.sdh.control.SanDaHaControl;
	import com.tangchao.client.sdh.definition.DFCardColor;
	import com.tangchao.client.sdh.definition.DFLayout;
	import com.tangchao.client.sdh.definition.DFState;
	import com.tangchao.client.sdh.definition.DFSuoFangDaXiao;
	import com.tangchao.client.sdh.definition.GlobalDefine;
	import com.tangchao.client.sdh.event.DaoGenFangEvent;
	import com.tangchao.client.sdh.event.JiaoFenEvent;
	import com.tangchao.client.sdh.event.PlayEvent;
	import com.tangchao.client.sdh.event.SelectColorEvent;
	import com.tangchao.client.sdh.sdhinterface.In;
	import com.tangchao.client.sdh.sdhinterface.Out;
	import com.tangchao.client.sdh.sdhinterface.Test;
	import com.tangchao.client.sdh.view.card.CardGrid;
	import com.tangchao.client.sdh.view.card.LeftOutCard;
	import com.tangchao.client.sdh.view.card.RightOutCard;
	import com.tangchao.client.sdh.view.card.SelfOutCard;
	import com.tangchao.client.sdh.view.card.UpOutCard;
	import com.tangchao.client.sdh.view.jiaoFen.JiaoFenPanel;
	import com.tangchao.client.sdh.view.role.Clock;
	import com.tangchao.client.sdh.view.role.RoleGrid;
	import com.tangchao.client.sdh.view.selectColor.BaseColor;
	import com.tangchao.client.sdh.view.selectColor.SelectColorPanel;
	import com.tangchao.client.sdh.vo.JieSuanVo;
	import com.tangchao.client.sdh.vo.Player;
	import com.tangchao.client.sdh.vo.RoleInfoVo;
	
	import fl.motion.Color;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import gs.TweenMax;

	public class SanDaHaPanel extends MovieClip
	{
		private var _roleInfo:RoleInfo;
		private var _numInfo:NumInfo;
		private var _daoGenFang:MovieClip;
		private var _readyPlay:ReadyPlay;
		private var _cardGrid:CardGrid;
		private var _dipaiCard:CardGrid;
		private var _selfOutCard:SelfOutCard;
		private var _leftOutCard:LeftOutCard;
		private var _rightOutCard:RightOutCard;
		private var _upOutCard:UpOutCard;
		private var _jiaoFen:JiaoFenPanel;
		private var _roleGrid:RoleGrid;
		private var _selectColor:SelectColorPanel;
		private var _clock:Clock;
		private var _jiesuan:JieSuan;
		private var _tfText:TextField;
		private var _nowState:int;
		private var _mcSuofang:SuoFangDaXiao;
		private var _mcBg:MCBG;
		
		private var _ready:MovieClip;
		private var _daoGenFanStat:MovieClip;
		private var _mcDizhu:MovieClip;
		private var _mcTuoguan:MovieClip;
		
		private var _tfType:TextField;
		private var _tfParam:TextField;
		private var _tfParam2:TextField;
		private var _btnOk:SimpleButton;
		
		private var _selectedMainColor:MovieClip;
		
		private var _difenSet:DiFenSet;
		
		private var _mainUserName:String;
		private var _mainSeatNo:int=-1;
		private var _playerArr:Object;
		private var _diPaiArr:Array;
		private var _mainCardColor:int = DFCardColor.COLOR_NONE;
		
		static private const POS_UP:int = 0;
		static private const POS_RIGHT:int = 1;
		static private const POS_DOWN:int = 2;
		static private const POS_LEFT:int = 3;
		
		static private const PLAY_STAT_END:int = 2;
		static private const PLAY_STAT_ROUND_END:int = 1;
		static private const PLAY_STAT_NORMAL:int = 0;
		
		static private var _instance:SanDaHaPanel;
		
		private var _isRoundBeg:Boolean = true;
		private var _jiesuanArr:Array;
		private var _suofangStat:int = DFSuoFangDaXiao.ZHONG;
				
		public function SanDaHaPanel()
		{
			init();
		}
		
		private function init()
		{
			_tfText=getChildByName("tfState") as TextField;
			_roleInfo=getChildByName("mcRoleInfo") as RoleInfo;
			_numInfo=getChildByName("mcNumInfo") as NumInfo;
			_daoGenFang=getChildByName("mcDaoGenFang") as MovieClip;
			_daoGenFang.stop();
			_daoGenFang.visible=false;
			_daoGenFang.addEventListener(MouseEvent.CLICK, onDaoGenFang);
			_readyPlay=getChildByName("mcReadyPlay") as ReadyPlay;
			_readyPlay.visible = false;
			_cardGrid=getChildByName("mcCardGrid") as CardGrid;
			_selfOutCard=getChildByName("mcSelfOutCard") as SelfOutCard;
			_leftOutCard=getChildByName("mcLeftOutCard") as LeftOutCard;
			_rightOutCard=getChildByName("mcRightCard") as RightOutCard;
			_upOutCard=getChildByName("mcUpOutCard") as UpOutCard;
			_jiaoFen=getChildByName("mcJiaoFen") as JiaoFenPanel;
			_jiaoFen.visible=false;
			_roleGrid=getChildByName("mcRoleGrid") as RoleGrid;
//			_roleGrid.setViewPos(_suofangStat);
			_dipaiCard=getChildByName("mcDiPai") as CardGrid;
			_selectColor=getChildByName("mcSelectColor") as SelectColorPanel;
			_selectColor.parentUI = this;
			_selectColor.visible=false;
			_clock=getChildByName("mcClock") as Clock;
			addEventListener(PlayEvent.OUT,onOutCard);
			addEventListener(PlayEvent.GAIPAI,onGaiPai);
			
			_ready=getChildByName("mcReady") as MovieClip;
			_ready.visible=false;
			_mcDizhu = getChildByName("mcDizhu") as MovieClip;
			_mcDizhu.visible = false;
			_daoGenFanStat = getChildByName("mcDaoGenFan") as MovieClip;
			_daoGenFanStat.stop();
			_daoGenFanStat.visible = false;
			_mcTuoguan = getChildByName("mcTuoguan") as MovieClip;
			_mcTuoguan.visible = false;
			
			_mcSuofang = getChildByName("mcSuofang") as SuoFangDaXiao;
			_mcSuofang.setParentUI(this);
			_mcBg      = getChildByName("mcBg") as MCBG;
			_mcBg.setParentUI(this);
			_mcBg.gotoAndStop(10);
			
			_playerArr  = new Object;
			_instance = this;
			
			
			_tfType   = getChildByName("tfType") as TextField;
			_tfType.restrict = "0-9";
			_tfParam  = getChildByName("tfParam") as TextField;
			_tfParam2 = getChildByName("tfParam2") as TextField;
			_btnOk    = getChildByName("btnOK") as SimpleButton;
			_btnOk.addEventListener(MouseEvent.CLICK, doTest);
			
			suofangEnd();
		}
		
		private function doTest(e:MouseEvent):void
		{
			var test:Test = Test.getInstance();
			test.doTest(_tfType.text, _tfParam.text, _tfParam2.text);
		}
		
		
		static public function getInstance():SanDaHaPanel
		{
			return _instance;
		}
		
		/*
		*显示结算窗
		*/
		public function showJieSuan():void
		{
			if(_jiesuan)
			{
				removeChild(_jiesuan);
				_jiesuan = null;
			}
			_jiesuan = new JieSuan;
			_jiesuan.parentUI = this;
			_jiesuan.x = 123;
			_jiesuan.y = 89;
			_jiesuan.update(_jiesuanArr);
			addChild(_jiesuan);
		}
		
		/*
		*用户自己闹钟显示并进入倒计时,value为倒计时总秒数
		*/
		public function showClock(value:int, func:Function=null)
		{
			_clock.setTimer(value, func);
			_clock.startTimer();
		}
		
		/*
		*隐藏用户自己的闹钟
		*/
		public function hideClock()
		{
			_clock.stopTimer();
		}
		
		/*
		*用户点击倒跟反，发送点击消息给前端逻辑
		*/
		private function onDaoGenFang(e:MouseEvent):void
		{
			var evt:DaoGenFangEvent=new DaoGenFangEvent();
			evt.index=_daoGenFang.currentFrame;
			dispatchEvent(evt);
		}
		
		public function onUserDao(userName:String):void
		{
			if(userName == _mainUserName)
			{
				_daoGenFanStat.visible = true;
				_daoGenFanStat.gotoAndStop(1);
				return;
			}
			
			var player:Player = _playerArr[userName] as Player;
			if(player)
			{
				player.doDao();
			}
			else
			{
				SanDaHaControl.getInstance().setMsg("该玩家不在游戏:"+userName);
			}
		}
		
		public function onUserGen(userName:String):void
		{
			if(userName == _mainUserName)
			{
				_daoGenFanStat.visible = true;
				_daoGenFanStat.gotoAndStop(2);	
				return;
			}
			
			var player:Player = _playerArr[userName] as Player;
			if(player)
			{
				player.doGen();
			}
			else
			{
				SanDaHaControl.getInstance().setMsg("该玩家不在游戏:"+userName);
			}
		}
		
		public function onUserFan(userName:String):void
		{
			if(userName == _mainUserName)
			{
				_daoGenFanStat.visible = true;
				_daoGenFanStat.gotoAndStop(3);
				return;
			}
			
			var player:Player = _playerArr[userName] as Player;
			if(player)
			{
				player.doFan();
			}
			else
			{
				SanDaHaControl.getInstance().setMsg("该玩家不在游戏:"+userName);
			}
		}
		
		public function onUserTuoGuan(userName:String, isTuoGuan:Boolean):void
		{
			if(userName == _mainUserName)
			{
				if(isTuoGuan)
				{
					_mcTuoguan.visible = true;
					_daoGenFang.gotoAndStop(4);
				}
				else
				{
					_mcTuoguan.visible = false;
					_daoGenFang.gotoAndStop(5);
				}
				return;
			}
			
			var player:Player = _playerArr[userName] as Player;
			if(player)
			{
				player.doTuoGuan(isTuoGuan);
			}
			else
			{
				SanDaHaControl.getInstance().setMsg("该玩家不在游戏:"+userName);
			}
		}
		
		
		/*
		*隐藏倒跟反 1-dao 2-gen 3-fan
		*/
		public function hideDaoGenFang(type:int=1, isHide:Boolean=true):void
		{
			if(type<1 || type>3)
			{
				SanDaHaControl.getInstance().setMsg("错误的倒跟反类型(1,2,3):"+type.toString());
				return;
			}
			_daoGenFang.gotoAndStop(type);
			_daoGenFang.visible=!isHide;
		}
		
		/*
		*显示或隐藏选择主牌框，true为显示，false为隐藏
		*/
		public function isShowSelectColor(b:Boolean)
		{
			_selectColor.visible=b;
			_selectColor.setStat(b);
		}
		
		/*
		*出牌消息加入选择的牌
		*/
		private function onOutCard(e:PlayEvent)
		{
			e.stopImmediatePropagation();
			var evt:PlayEvent=new PlayEvent(PlayEvent.OUT_CARD);
			evt.card=_cardGrid.nowSelectCard;
			dispatchEvent(evt);
		}
		
		/*
		*盖牌消息加入选择的牌
		*/
		private function onGaiPai(e:PlayEvent)
		{
			e.stopImmediatePropagation();
			if(_cardGrid.nowSelectCard.length==8)  //若选择的牌数为8，则发生盖牌消息
			{
				_cardGrid.selectCard = null;
				var evt:PlayEvent=new PlayEvent(PlayEvent.GAIPAI_CARD);
				evt.card=_cardGrid.nowSelectCard;
				dispatchEvent(evt);
			}
			else
			{
				SanDaHaControl.getInstance().setMsg("请选择8张");
			}
		}
		
		/*
		*显示底牌，请将牌从大到小排列.array中每个都为代表一个牌的数字
		*/
		public function updataDiPaiCard(arr:Array)
		{
			_dipaiCard.create(arr);
			_dipaiCard.x=DFLayout.X_MID-_dipaiCard.width/2;
			TweenMax.to(_dipaiCard, GlobalDefine.DIPAISHOWTIME, {alpha:0, onComplete:showJieSuan});
		}
		
		/*
		*隐藏底牌
		*/
		public function hideDiPaiCard()
		{
			_dipaiCard.visible=false;
		}
		
		/*
		*删除底牌
		*/
		public function deleteDiPaiCard()
		{
			_dipaiCard.destroy();
		}
		
		/*
		*创建或删除或更改左边用户信息接口:若无用户或删除用户，传入null；若创建或更改用户信息传入RoleInfoVo
		*/
		public function leftRoleIn(v:Player)
		{
			_roleGrid.leftRoleIn(v);
		}
		
		/*
		*创建或删除或更改右边用户信息接口:若无用户或删除用户，传入null；若创建或更改用户信息传入RoleInfoVo
		*/
		public function rightRoleIn(v:Player)
		{
			_roleGrid.rightRoleIn(v);
		}
		
		/*
		*创建或删除或更改上边用户信息接口:若无用户或删除用户，传入null；若创建或更改用户信息传入RoleInfoVo
		*/
		public function upRoleIn(v:Player)
		{
			_roleGrid.upRoleIn(v);
		}
		
		public function onUserEnterRoom(userName:String, arr:Array):void
		{
			var i:int;
			var user:User;
			var pos:int;
			var player:Player;
			var findMianUser:Boolean = false;
			
			if(!_mainUserName)
			{
				SanDaHaControl.getInstance().setMsg("主玩家未设置");
			}
			
			for(i-0; i<arr.length; i++)
			{
				user = arr[i] as User;
				if(user && user.getName() == _mainUserName)
				{
					findMianUser = true;
					_mainSeatNo = user.getSeatNo();
				}
			}
			
			if(!findMianUser)
			{
				SanDaHaControl.getInstance().setMsg("主玩家不在房间");
			}
			
			if(arr)
			{
				clearAllPlayer();
				for(i=0; i<arr.length; i++)
				{
					user = arr[i] as User;
					if(user)
					{
						if(user.getName() == _mainUserName)
						{
							continue;
						}
						
						player = new Player;
						pos = getUserPosBySeatNo(user.getSeatNo());
						player.user = user;
						_playerArr[player.user.getName()] = player;
						
						switch(pos)
						{
							case POS_UP:
								upRoleIn(player);
								break;
							case POS_RIGHT:
								rightRoleIn(player);
								break;
							case POS_LEFT:
								leftRoleIn(player);
								break;
							default:
								break;
						}
					}
				}
			}
		}
		
		private function clearAllPlayer():void
		{
			var player:Player;
			if(_playerArr)
			{
				for each(player in _playerArr)
				{
					player.destory();
				}
			}
		}
		
		public function userLeaveRoom(userName:String, userArr:Array):void
		{
			var player:Player;
			var i:int;
			var name:String;
			
			if(userName)
			{
				player = _playerArr[userName] as Player;
				if(player)
				{
					player.destory();
					delete _playerArr[userName];
				}
			}
		}
		
		//返回 1-up 2-right 3-left
		private function getUserPosBySeatNo(seatNo:int):int
		{
			var val:int = seatNo-_mainSeatNo;
			var ret:int;
			
			switch(val)
			{
				case 1:
				case -3:
					ret = POS_LEFT;
					break;
				case -2:
				case 2:
					ret = POS_UP;
					break;
				case -1:
				case 3:
					ret = POS_RIGHT;
					break;
				default:
					break;
			}
			return ret;
		}
		
		//玩家准备
		public function onUserReady(userName:String, userArr:Array, allReady:Boolean):void
		{
			var player:Player;
			var i:int;
			var name:String;
			
			if(userArr)
			{
				for each(player in _playerArr)
				{
					player.setStat(DFState.READY_NO);
				}
								
				for(i=0; i<userArr.length; i++)
				{
					name = userArr[i] as String;
					if(name)
					{
						if(name == _mainUserName)
						{
							_readyPlay.nowState = DFState.NONE_STAT;
							_ready.visible = true;
							continue;
						}
						player = _playerArr[name] as Player;
						if(player)
						{
							player.setStat(DFState.READY_YES);
						}
					}
				}
			}
		}
		
		/*
		*自己出牌，请将牌从大到小排列.array中每个都为代表一个牌的数字
		*/
		public function selfOut(arr:Array)
		{
			_selfOutCard.create(arr);
			_selfOutCard.x=DFLayout.X_MID-_selfOutCard.width/2;
		}
		
		/*
		*左边角色出牌，请将牌从大到小排列.array中每个都为代表一个牌的数字
		*/
		public function leftOut(arr:Array)
		{
			_leftOutCard.create(arr);
		}
		
		/*
		*右边角色出牌，请将牌从大到小排列.array中每个都为代表一个牌的数字
		*/
		public function rightOut(arr:Array)
		{
			_rightOutCard.create(arr);
			_rightOutCard.x=_rightOutCard.x-_rightOutCard.width+40;
		}
		
		/*
		*上边角色出牌，请将牌从大到小排列.array中每个都为代表一个牌的数字
		*/
		public function upOut(arr:Array)
		{
			_upOutCard.create(arr);
			_upOutCard.x=DFLayout.X_MID+20-_upOutCard.width/2;
		}
		
		public function onFaPai(paiArr:Array, userName:String, maxScore:int, mustBet:Boolean):void
		{
			var player:Player;
			
			_ready.visible = false;
			updataSelfCard(paiArr);
			
			if(userName == _mainUserName)
			{
				jiaofen(maxScore, mustBet);
				_readyPlay.nowState = DFState.READY_YES;
			}
			
			player = _playerArr[userName] as Player;
			if(player)
			{
				player.tryBet(maxScore);
			}
		}
		
		/*
		*设置自己的牌，请将牌从大到小排列.array中每个都为代表一个牌的数字
		*/
		public function updataSelfCard(arr:Array)
		{
			_cardGrid.create(arr);
			_cardGrid.x=DFLayout.X_MID-_cardGrid.width/2;
		}
		
		/*
		*轮到自己出牌
		*/
		public function isOutCard(val:Boolean=true):void
		{
			if(val)
			{
				_readyPlay.nowState=DFState.PLAY_OUT;
			}
			else
			{
				_readyPlay.nowState = DFState.NONE_STAT;
			}
		}
		
		/*
		*用户更改用户状态
		*/
		public function set nowState(i:int)
		{
			_nowState=i;
			_readyPlay.nowState=_nowState;
			switch(_nowState)
			{
				case DFState.READY_NO:
					_tfText.text="状态:未准备";
					break;
				case DFState.READY_YES:
					doReadyYes();;
					break;
				case DFState.PLAY_END_FANGPAI:
					_tfText.text="状态:最终翻拍";
					break;
				case DFState.PLAY_END_RESULT:
					_tfText.text="状态:结算";
					break;
				case DFState.PLAY_SLELECTCOLOR:
					_tfText.text="状态:选主牌";
					break;
				case DFState.PLAY_SOUNDNUM:
					doJiaofen();
					break;
				case DFState.PLAY_WAITSOUND:
					_tfText.text="状态:等待叫分";
					break;
				case DFState.PLAY_WAIT:
					_tfText.text="状态:别人出牌";
					break;
				case DFState.PLAY_OUT:
					_tfText.text="状态:自己出牌";
					isOutCard();
					break;
				case DFState.PLAY_GAIPAI:
					_tfText.text="状态:盖底牌";
					_readyPlay.nowState=DFState.PLAY_GAIPAI;
					break;
				case DFState.PLAY_WAITGAIPAI:
					_tfText.text="状态:等待别人盖底牌";
					break;
				default:
					_tfText.text="等待";
					break;
			}
		}
		
		private function doReadyYes():void
		{
			hideClock();
			SanDaHaControl.getInstance().setMsg("当前玩家已准备");
		}
		
		private function doJiaofen():void
		{
			SanDaHaControl.getInstance().setMsg("进入叫分阶段");
		}
		
		/*
		*叫分接口，value为用户当前最高叫几分
		*/
		public function jiaofen(value:int, mustBet:Boolean)
		{
			_jiaoFen.visible=true;
			_jiaoFen.setMaxNum(value, mustBet);
			showClock(GlobalDefine.JIAOFENWAITSEC, onJiaofenTimerOver);
		}
		
		//叫分
		public function userBet(userName:String, value:int, isJiaofenEnd:Boolean, param:Array):void
		{
			var player:Player;
			var zjName:String;
			var userCars:Array;
			
			player = _playerArr[userName] as Player;
			_readyPlay.nowState = DFState.NONE_STAT;
			
			if(player)
			{
				player.doBet(value);
			}
			if(isJiaofenEnd)
			{
				createSoundNum(value);
				SanDaHaControl.getInstance().setMsg("叫分结束:"+value.toString());
				zjName = param[0] as String;
				if(zjName != _mainUserName)
				{
					player = _playerArr[userName] as Player;
					if(player)
					{
						player.setZJ(true); 
					}
				}
				else
				{
					setZJSelf(true);
					userCars = param[2] as Array;
					if(userCars)
					{
						updataSelfCard(userCars);
					}
					isShowSelectColor(true);
				}
				_diPaiArr = param[1] as Array;
			}
			else
			{
				var nameTmp:String = param[0] as String;
				var maxNun:int     = param[1] as int;
				var mustBet:Boolean= param[2] as Boolean;
				if(nameTmp == _mainUserName)
				{
					jiaofen(maxNun, mustBet);
					showClock(GlobalDefine.JIAOFENWAITSEC, onJiaofenTimerOver);
				}
				else
				{
					player = _playerArr[nameTmp] as Player;
					if(player)
					{
						player.tryBet(param[1] as int);
					}
				}
			}
		}
		
		public function onJiaofenTimerOver():void
		{
			hideClock();
			_jiaoFen.doAutoBet();
		}
		
		private function setZJSelf(val:Boolean):void
		{
			_mcDizhu.visible = true;
		}
		
		/*
		*隐藏叫分框
		*/
		public function hideJiaoFen()
		{
			_jiaoFen.visible=false;
		}
		
		/*
		*创建自己信息
		*/
		public function createRoleInfo(v:User)
		{
			if(v)
			{
				_mainUserName = v.getName();
				_mainSeatNo   = v.getSeatNo();
				_roleInfo.create(v);
				Test.getInstance().doTest("1", _mainUserName, "0");
			}
			onGameReset();
		}
		
		public function onGameReset(difen:int = 0):void
		{
			_dipaiCard.destroy();
			_mainCardColor = DFCardColor.COLOR_NONE;
			if(_mainCardColor)
			{
				removeChild(_selectedMainColor);
				_selectedMainColor = null;
			}
			if(_jiesuan)
			{
				removeChild(_jiesuan);
				_jiesuan = null;
			}
			_diPaiArr = null;
			_daoGenFang.visible=false;
			_readyPlay.visible = false;
			_jiaoFen.visible=false;
			_selectColor.visible=false;
			_ready.visible=false;
			_mcDizhu.visible = false;
			_mcTuoguan.visible = false;
			_daoGenFanStat.visible = false;
			_readyPlay.nowState = DFState.READY_NO;
		}
		
		/*
		*创建叫分的分数
		*/
		public function createSoundNum(value:int)
		{
			_numInfo.createSoundNum(value);
		}
		
		/*
		*更新获得的分数
		*/
		public function updataGetNum(value:int)
		{
			_numInfo.updataGetNum(value);
		}
		
		/*
		*创建主牌,1为方块,2为红桃,3为草花，4为黑桃
		*/
		public function createMainCard(userName:String, color:int)
		{
			if(userName == _mainUserName)
			{
				_readyPlay.nowState = DFState.PLAY_GAIPAI;
			}
			else
			{
				switch(color)
				{
					case DFCardColor.COLOR_CAOHUA:
						_selectedMainColor = new ColorCaohua;
						break;
					case DFCardColor.COLOR_FANGKUAI:
						_selectedMainColor = new ColorFangkuai;
						break;
					case DFCardColor.COLOR_HEITAO:
						_selectedMainColor = new ColorHeitao;
						break;
					case DFCardColor.COLOR_HONGTAO:
						_selectedMainColor = new ColorHongtao;
						break;	
					default:
						break;
				}
				
				if(_selectedMainColor && _numInfo)
				{
					var pt:Point = _numInfo.getGlobalMianCardPos();
					_selectedMainColor.x = pt.x;
					_selectedMainColor.y = pt.y;
					_selectedMainColor.width = 36;
					_selectedMainColor.height = 45;
					addChild(_selectedMainColor);
				}
			}
		}
		
		public function userPutBottomCards(userName:String, cardArr:Array):void
		{
			_diPaiArr = cardArr;
			_daoGenFang.gotoAndStop(5);
			if(userName == _mainUserName)
			{
//				if(_cardGrid)
//				{
//					_cardGrid.delCards(cardArr);
//				}
				_readyPlay.nowState = DFState.PLAY_OUT;
				showClock(GlobalDefine.OUTCARDSEC, cardAutoOut);
			}
			else
			{
				var player:Player = _playerArr[userName] as Player;
				if(player)
				{
					player.tryPlayOut();
				}
			}
		}
		
		//userName-出牌用户名 cardArr-出的牌 cpRet-false为甩牌失败
		public function userPlayCards(userName:String, cardArr:Array, cpRet:Boolean, stat:int, param:Array):void
		{
			// wait for impelement
			if(userName == _mainUserName)
			{
				selfOut(cardArr);
			}
			else
			{
				var player:Player = _playerArr[userName] as Player;
				if(player)
				{
					player.stopClock();
					var pos:int = getUserPosBySeatNo(player.user.getSeatNo());
					switch(pos)
					{
						case POS_UP:
							upOut(cardArr);
							break;
						case POS_LEFT:
							leftOut(cardArr);
							break;
						case POS_RIGHT:
							rightOut(cardArr);
							break;
						default:
							break;
					}
				}	
			}
			
			if(_isRoundBeg)
			{
				outCardsReset();
			}
			
			switch(stat)
			{
				case PLAY_STAT_ROUND_END:
					tryPlayOut(param[0] as String);
					_isRoundBeg = true;
					break;
				case PLAY_STAT_END:
					outCardsReset();
					updataDiPaiCard(cardArr);
					_jiesuanArr = param;
//					showJieSuan(param);
					_isRoundBeg = true;
					break;
				case PLAY_STAT_NORMAL:
					tryPlayOut(param[0] as String);
					_isRoundBeg = false;
					break;
				default:
					break;
			}
		}
		
		private function outCardsReset():void
		{
			_selfOutCard.destroy();
			_upOutCard.destroy();
			_leftOutCard.destroy();
			_rightOutCard.destroy();
		}
		
		private function tryPlayOut(userName:String):void
		{
			if(userName == _mainUserName)
			{
				_readyPlay.nowState = DFState.PLAY_OUT;
				_clock.setTimer(GlobalDefine.OUTCARDSEC, cardAutoOut);
				return;
			}
			var player:Player =  _playerArr[userName] as Player;
			if(player)
			{
				player.tryPlayOut();
			}
		}
		
		public function cardAutoOut():void
		{
			Out.getInstance().doAotuOut(_cardGrid.nowSelectCard);
		}
		
		public function onSelectColor(color:int, posX:int, posY:int):void
		{			
			_mainCardColor = color;
			switch(color)
			{
				case DFCardColor.COLOR_CAOHUA:
					_selectedMainColor = new ColorCaohua;
					break;
				case DFCardColor.COLOR_FANGKUAI:
					_selectedMainColor = new ColorFangkuai;
					break;
				case DFCardColor.COLOR_HEITAO:
					_selectedMainColor = new ColorHeitao;
					break;
				case DFCardColor.COLOR_HONGTAO:
					_selectedMainColor = new ColorHongtao;
					break;	
				default:
					break;
			}
			if(_selectedMainColor && _numInfo)
			{
				_selectedMainColor.x = posX;
				_selectedMainColor.y = posY;
				addChild(_selectedMainColor);
				var pt:Point = _numInfo.getGlobalMianCardPos();
				
				if(_selectColor)
				{
					_selectColor.visible = false;
				}
				TweenMax.to(_selectedMainColor, GlobalDefine.SELECTMAINCARDSEC, {x:pt.x, y:pt.y, width:36, height:45, onComplete:doSelectMainCard});
			}
		}
		
		public function doSelectMainCard():void
		{
			var evt:SelectColorEvent = new SelectColorEvent();
			evt.color=_mainCardColor;
			dispatchEvent(evt);
		}
		
		public function setHandCards(cardArr:Array):void
		{
			if(!cardArr)
			{
				return;
			}
			
			updataSelfCard(cardArr);
		}
		
		public function onSetViewPos(viewPos:int):void
		{
			if(viewPos == _suofangStat)
			{
				return;
			}
			
			switch(viewPos)
			{
				case DFSuoFangDaXiao.JIN:
					if(_suofangStat == DFSuoFangDaXiao.YUAN)
					{
						suofangPre();
						_mcBg.gotoAndPlay(41);
						_suofangStat = viewPos;
					}
					else if(_suofangStat == DFSuoFangDaXiao.ZHONG)
					{
						suofangPre();
						_mcBg.gotoAndPlay(11);
						_suofangStat = viewPos;
					}
					break;
				case DFSuoFangDaXiao.YUAN:
					if(_suofangStat == DFSuoFangDaXiao.JIN)
					{
						suofangPre();
						_mcBg.gotoAndPlay(51);
						_suofangStat = viewPos;
					}
					else if(_suofangStat == DFSuoFangDaXiao.ZHONG)
					{
						suofangPre();
						_mcBg.gotoAndPlay(31);
						_suofangStat = viewPos;
					}
					break;
				case DFSuoFangDaXiao.ZHONG:
					if(_suofangStat == DFSuoFangDaXiao.JIN)
					{
						suofangPre();
						_mcBg.gotoAndPlay(21);
						_suofangStat = viewPos;
					}
					else if(_suofangStat == DFSuoFangDaXiao.YUAN)
					{
						suofangPre();
						_mcBg.gotoAndPlay(2);
						_suofangStat = viewPos;
					}
					break;
				default:
					break;
			}
		}
		
		private function suofangPre():void
		{
			if(_roleGrid)
			{
				_roleGrid.visible = false;
			}
		}
		
		public function suofangEnd():void
		{
//			var player:Player;
//			for each(player in _playerArr)
//			{
//				player.setViewPos(_suofangStat);
//			}
			
			if(_roleGrid)
			{
				_roleGrid.setViewPos(_suofangStat);
				_roleGrid.visible = true;
			}
		}
		
		public function userSurrender(name:String, ret:Array):void
		{
			var nameTmp:String;
			var cardsTmp:Array;
			var player:Player;
			var pos:int
			
			if(ret)
			{
				_jiesuanArr = ret;
				var mcTmp:MovieClip = new MovieClip;
				addChild(mcTmp);
				TweenMax.to(mcTmp, GlobalDefine.SHOWHANDCARDSSEC, {alpha:0, onComplete:showJieSuan});
				
				nameTmp  = ret[1] as String;
				cardsTmp = ret[2] as Array;			
				if(nameTmp && cardsTmp)
				{
					if(nameTmp != _mainUserName)
					{
						player = _playerArr[nameTmp] as Player;
						var pos:int = getUserPosBySeatNo(player.user.getSeatNo());
						switch(pos)
						{
							case POS_UP:
								upOut(cardsTmp);
								break;
							case POS_LEFT:
								leftOut(cardsTmp);
								break;
							case POS_RIGHT:
								rightOut(cardsTmp);
								break;
							default:
								break;
						}
					}
				}
				
				nameTmp  = ret[4] as String;
				cardsTmp = ret[5] as Array;
				if(nameTmp && cardsTmp)
				{
					if(nameTmp != _mainUserName)
					{
						player = _playerArr[nameTmp] as Player;
						var pos:int = getUserPosBySeatNo(player.user.getSeatNo());
						switch(pos)
						{
							case POS_UP:
								upOut(cardsTmp);
								break;
							case POS_LEFT:
								leftOut(cardsTmp);
								break;
							case POS_RIGHT:
								rightOut(cardsTmp);
								break;
							default:
								break;
						}
					}
				}
				
				nameTmp  = ret[7] as String;
				cardsTmp = ret[8] as Array;
				if(nameTmp && cardsTmp)
				{
					if(nameTmp != _mainUserName)
					{
						player = _playerArr[nameTmp] as Player;
						var pos:int = getUserPosBySeatNo(player.user.getSeatNo());
						switch(pos)
						{
							case POS_UP:
								upOut(cardsTmp);
								break;
							case POS_LEFT:
								leftOut(cardsTmp);
								break;
							case POS_RIGHT:
								rightOut(cardsTmp);
								break;
							default:
								break;
						}
					}
				}
				
				nameTmp  = ret[10] as String;
				cardsTmp = ret[11] as Array;
				if(nameTmp && cardsTmp)
				{
					if(nameTmp != _mainUserName)
					{
						player = _playerArr[nameTmp] as Player;
						var pos:int = getUserPosBySeatNo(player.user.getSeatNo());
						switch(pos)
						{
							case POS_UP:
								upOut(cardsTmp);
								break;
							case POS_LEFT:
								leftOut(cardsTmp);
								break;
							case POS_RIGHT:
								rightOut(cardsTmp);
								break;
							default:
								break;
						}
					}
				}
			}
		}
		
		public function setSelectedCards(cardArr:Array):void
		{
			if(_cardGrid && cardArr)
			{
				_cardGrid.selectCard = cardArr;
			}
		}
		
		public function enterSetMode():void
		{
			_difenSet = new DiFenSet;
			addChild(_difenSet);
			_difenSet.x = 557;
			_difenSet.y = 388;
		}
		
		private function removeDifenSet():void
		{
			if(_difenSet)
			{
				removeChild(_difenSet);
				_difenSet = null;
			}
		}
		
		public function userSetScoreModel(scoreName:String, mode:int, score:int, modeName:String, isAllAplly:Boolean):void
		{
			if(isAllAplly)
			{
				removeDifenSet();
				return;
			}
						
			if(!_difenSet)
			{
				enterSetMode();
			}
			
			if(_difenSet)
			{
				_difenSet.userSetScoreModel(scoreName, mode, score, modeName, isAllAplly, scoreName==_mainUserName);
			}
		}
	}
}